package com.heima.article.controller.v1;

import com.heima.article.service.ApAuthorService;
import com.heima.article.service.ArticleService;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.pojos.ApArticle;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 嘉德罗斯
 * @date 2021/11/19
 */

@RestController
@RequestMapping("/api/v1/article")
@Api(value = "app端用户主页展示",tags = "app端用户主页展示")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private ApAuthorService apAuthorService;

    @PostMapping("/load")
    public ResponseResult load(@RequestBody ArticleHomeDto dto){
        List<ApArticle> apArticles = articleService.load(dto);
        List<Long> ids = new ArrayList<>();
        for (ApArticle record : apArticles) {
            ids.add(record.getAuthorId());
        }
        Map<Long,String> map = apAuthorService.load(ids);
        for (ApArticle apArticle : apArticles) {
            apArticle.setAuthorName(map.get(apArticle.getAuthorId()));
        }
        ResponseResult<Object> responseResult = new ResponseResult<>();
        responseResult.setCode(200);
        responseResult.setData(apArticles);
        return responseResult;
    }

    @PostMapping("/loadnew")
    public ResponseResult loadnew(@RequestBody ArticleHomeDto dto){
        return load(dto);
    }
}
